package com.junxonline.instant.admin.controller.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.junxonline.instant.dao.entity.model.config.Organization;
import com.junxonline.instant.dao.entity.vo.common.IdsVO;
import com.junxonline.instant.dao.entity.vo.config.OrganizationVO;
import com.junxonline.instant.admin.service.config.OrganizationService;
import com.junxonline.instant.common.annotation.OperationLog;
import com.junxonline.instant.common.entity.common.CommonResult;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.enumeration.OperationEnum;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 组织机构管理Controller
 *
 * @author JunX
 * @date 2021-07-05
 */
@Log4j2
@RestController
@RequestMapping(value = "/config/organization")
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;

    /**
     * 查询组织机构树（TreeSelect）
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:organization:query")
    @OperationLog(label = "组织机构管理-查询组织机构树", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryOrganizationTree")
    public CommonResult queryOrganizationTree(@RequestBody(required = false) OrganizationVO vo) {
        return CommonResult.success(organizationService.queryOrganizationTree(new QueryWrapper<>()));
    }

    /**
     * 查询上级组织机构树（TreeSelect）
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:organization:query")
    @OperationLog(label = "组织机构管理-查询上级组织机构树", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryParentTree")
    public CommonResult queryParentTree(@RequestBody(required = false) OrganizationVO vo) {
        return CommonResult.success(organizationService.queryParentTree(vo, new QueryWrapper<>()));
    }

    /**
     * 查询组织机构列表
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:organization:query")
    @OperationLog(label = "组织机构管理-查询列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryList")
    public CommonResult queryList(@RequestBody(required = false) OrganizationVO vo) {
        return CommonResult.success(organizationService.queryList(vo, new QueryWrapper<>()));
    }

    /**
     * 查询组织机构
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:organization:query")
    @OperationLog(label = "组织机构管理-查询组织机构", type = OperationEnum.QUERY)
    @PostMapping(value = "/query")
    public CommonResult query(@RequestBody(required = false) OrganizationVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(organizationService.query(vo.getId()));
    }

    /**
     * 新增组织机构
     *
     * @param organization 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:organization:add")
    @OperationLog(label = "组织机构管理-新增组织机构", type = OperationEnum.ADD)
    @PostMapping(value = "/add")
    public CommonResult add(@RequestBody(required = false) Organization organization) {
        if (organization == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        organizationService.add(organization);
        return CommonResult.success();
    }

    /**
     * 编辑组织机构
     *
     * @param organization 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:organization:edit")
    @OperationLog(label = "组织机构管理-编辑组织机构", type = OperationEnum.EDIT)
    @PostMapping(value = "/edit")
    public CommonResult edit(@RequestBody(required = false) Organization organization) {
        if (organization == null || organization.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        // TODO 处理顶级平台特殊逻辑
        if (organization.getId() == 1 && organization.getStatus() == 0) {
            return CommonResult.fail(AdminErrorEnum.ORGANIZATION_PLATFORM_NOT_DISABLE);
        }
        organizationService.edit(organization);
        return CommonResult.success();
    }

    /**
     * 删除组织机构
     *
     * @param idsVO 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:organization:delete")
    @OperationLog(label = "组织机构管理-删除组织机构", type = OperationEnum.DELETE)
    @PostMapping(value = "/delete")
    public CommonResult delete(@RequestBody(required = false) IdsVO<List<Long>> idsVO) {
        if (idsVO == null || idsVO.getIds() == null || idsVO.getIds().size() <= 0) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        for (Long id : idsVO.getIds()) {
            // TODO 处理顶级平台特殊逻辑
            if (id == 1) {
                return CommonResult.fail(AdminErrorEnum.ORGANIZATION_PLATFORM_NOT_DISABLE);
            }
        }
        organizationService.delete(idsVO.getIds());
        return CommonResult.success();
    }

}
